#include <QByteArray>
#include <QXmlAttributes>
#include <QXmlStreamReader>
#include <QString>
#include <QDebug>
#include <QDateTime>

#include "_Interface/_global_datastruct.h"
#include "Backend/wyzkr688rfswbackend.h"
#include "Package/simpletcpserverdll_global.h"
#include "Package/simpletcpsocketdll_global.h"


wyzkr688rfswbackend::wyzkr688rfswbackend(){
    if(this->pgtrobotcomfsw == nullptr) pgtrobotcomfsw = new gtrobotcomfsw();
    if(this->pBasicRFSWTech == nullptr) pBasicRFSWTech = new BasicRFSWTech();

    this->mDataSaveParam.SaveFlagLast = false;
}
wyzkr688rfswbackend::~wyzkr688rfswbackend(){
    if(this->pgtrobotcomfsw != nullptr) delete this->pgtrobotcomfsw;
    if(this->pBasicRFSWTech != nullptr) delete this->pBasicRFSWTech;
}
short wyzkr688rfswbackend::toRobotTcpServerAdd(){
    return this->pgtrobotcomfsw->toRobotTcpServerAdd();
}
short wyzkr688rfswbackend::toRobotTcpServerListen(){
    return this->pgtrobotcomfsw->toRobotTcpServerListen();
}
bool  wyzkr688rfswbackend::getRobotConnectStatus(){
    return this->pgtrobotcomfsw->getRobotConnectStatus();
}
short wyzkr688rfswbackend::ReadRobotJointData(double (&data)[6]){
    return this->pgtrobotcomfsw->ReadRobotJointData(data);
}
short wyzkr688rfswbackend::ReadRobotJointTorData(double (&data)[6]){
    return this->pgtrobotcomfsw->ReadRobotJointTorData(data);
}
short wyzkr688rfswbackend::ReadSpindleSpeed(double& data){
    return this->pgtrobotcomfsw->ReadSpindleSpeed(data);
}

short wyzkr688rfswbackend::ReadForceControlForceSetValue(double &data){
    return this->pgtrobotcomfsw->ReadForceControlForceSetValue(data);
}
short wyzkr688rfswbackend::ReadForceControlForceRealValue(double &Data){
    return this->pgtrobotcomfsw->ReadForceControlForceRealValue(Data);
}
short wyzkr688rfswbackend::ReadForceControlInitForceSetValue(double &data){
    return this->pgtrobotcomfsw->ReadForceControlForceInitSetValue(data);
}
short wyzkr688rfswbackend::ReadInitPosDelayTime(int &data){
    return this->pgtrobotcomfsw->ReadInitPosDelayTime(data);
}
short wyzkr688rfswbackend::ReadRFSWStage(short &data){
    return this->pBasicRFSWTech->ReturnRSFSWStage(data);
}
short wyzkr688rfswbackend::ReadForceControlStep(double &data){
    return this->pgtrobotcomfsw->ReadForceControlStep(data);
}

short wyzkr688rfswbackend::RFSWPorcess(){
    bool tempbool = this->pgtrobotcomfsw->CanSendtoRobot;
    if( tempbool == true){
        ST_RobotDAQData tempRobotDAQData;
        ST_FswTecDAQData tempFswTecDAQData;
        ST_FswTecAdjData tempFswTecAdjData;
        tempRobotDAQData = this->pgtrobotcomfsw->mRobotDAQData;

        this->pgtrobotcomfsw->ReadInitPosDone(tempFswTecDAQData.InitPosDone);
        this->pgtrobotcomfsw->ReadEndPosDone(tempFswTecDAQData.EndPosDone);
        this->pgtrobotcomfsw->ReadForceControlForceInitSetValue(tempFswTecDAQData.ForceInitPosSet);
        this->pgtrobotcomfsw->ReadForceControlForceSetValue(tempFswTecDAQData.ForceProcessSet);
        this->pgtrobotcomfsw->ReadForceControlForceRealValue(tempFswTecDAQData.ForceSensorReal);
        this->pgtrobotcomfsw->ReadForceControlStep(tempFswTecDAQData.ForceAdjGlobalCoefficient);
        this->pgtrobotcomfsw->ReadInitPosDelayTime(tempFswTecDAQData.InitPosDelayTime);

        this->pBasicRFSWTech->GetRobobtandFswParamaters(tempRobotDAQData,tempFswTecDAQData);
        this->pBasicRFSWTech->RFSWRobotProcessBasic();
        this->pBasicRFSWTech->SetFswAdjData(tempFswTecAdjData);
        this->pgtrobotcomfsw->WriteRobotOVN(tempFswTecAdjData.RobotOvn);
        this->pgtrobotcomfsw->WriteRobotFzDirOffset(tempFswTecAdjData.RobotPosZOffsetAbs);
    }
    return 1;
}

short wyzkr688rfswbackend::toRobotreceiveLoopRun(){
    return this->pgtrobotcomfsw->toRobotReceiveLoopRun();
}
short wyzkr688rfswbackend::toRobotsendLoopRun(){
    return this->pgtrobotcomfsw->toRobotSendLoopRun();
}

short wyzkr688rfswbackend::ProcessDataSave(){
    bool AllowDataSave = false;
    bool AllDataSaveDone = false;
    this->pgtrobotcomfsw->ReadInitPosDone(AllowDataSave); //用达到初始点作为保存的依据
    this->pgtrobotcomfsw->ReadEndPosDone(AllDataSaveDone);

    if(AllowDataSave == true){
        if(AllDataSaveDone == false){
            if((this->mDataSaveParam.SaveFlagLast == false)){
                static QString FileSaveMessage;

                QDateTime current_date_time = QDateTime::currentDateTime();
                QString   current_time = current_date_time.toString("hh-mm-ss");
                QString   current_date = current_date_time.toString("yyyy-MM-dd-");
                QString   filename_data = "../" + current_date + current_time + "-data.csv";

                mDataSaveParam.mSaveFile = new QFile(filename_data);
                mDataSaveParam.mSaveFile->open(QIODevice::WriteOnly | QIODevice::Text);
                mDataSaveParam.mStreamData = new QTextStream(mDataSaveParam.mSaveFile);

                *(mDataSaveParam.mStreamData) << "Time" << ","
                    << "FzInit"    << ","
                    << "FzSet"     << ","
                    << "Fz"        <<","
                    << "DelayTime" <<","
                    << "Spindle"   << ","
                    << "OVN" << ","
                    << "J1"  << ","
                    << "J2"  << ","
                    << "J3"  << ","
                    << "J4"  << ","
                    << "J5"  << ","
                    << "J6"  << ","
                    << "J1T" << ","
                    << "J2T" << ","
                    << "J3T" << ","
                    << "J4T" << ","
                    << "J5T" << ","
                    << "J6T" << ","
                    << "OffsetY" << ","
                    << "OffsetZ" << "\n";
                mDataSaveParam.TimeOrderLast = QDateTime::currentDateTime().toMSecsSinceEpoch();
                FileSaveMessage = "&nbsp;&nbsp;&nbsp;&nbsp;" + filename_data;
                mDataSaveParam.SaveFlagLast = true;
            }
        }
        else{
            if((this->mDataSaveParam.SaveFlagLast == true)){
                mDataSaveParam.mSaveFile->close();
                delete mDataSaveParam.mSaveFile;
                delete mDataSaveParam.mStreamData;
                this->mDataSaveParam.SaveFlagLast = false;
            }
        }
    }

    if(AllowDataSave == true){
        if((this->mDataSaveParam.SaveFlagLast == true)){
            if(this->pgtrobotcomfsw->NewMessageBackendReadDone()){
                double ForceInitSet = 0;
                double ForceSet = 0;
                double ForceReal = 0;
                double Spindle = 0;
                int    DelayTime = 0;

                this->pgtrobotcomfsw->ReadForceControlForceInitSetValue(ForceInitSet);
                this->pgtrobotcomfsw->ReadForceControlForceSetValue(ForceSet);
                this->pgtrobotcomfsw->ReadForceControlForceRealValue(ForceReal);
                this->pgtrobotcomfsw->ReadSpindleSpeed(Spindle);
                this->pgtrobotcomfsw->ReadInitPosDelayTime(DelayTime);

                mDataSaveParam.TimeOrder = QDateTime::currentDateTime().toMSecsSinceEpoch();
                mDataSaveParam.TimeOrderNow = mDataSaveParam.TimeOrder - mDataSaveParam.TimeOrderLast;
                *(mDataSaveParam.mStreamData) << mDataSaveParam.TimeOrderNow << ","
                                              <<ForceInitSet                 << ","
                                              <<ForceSet                     << ","
                                              <<ForceReal                    << ","
                                              <<DelayTime                    << ","
                                              <<Spindle                      << ","
                                              <<this->pgtrobotcomfsw->mRobotAdjData.toRobotOvn            <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[0]     <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[1]     <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[2]     <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[3]     <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[4]     <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[5]     <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[0]  <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[1]  <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[2]  <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[3]  <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[4]  <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[5]  <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotTTSOffsetData[1] <<","
                                              <<this->pgtrobotcomfsw->mRobotDAQData.RobotTTSOffsetData[2] << "\n";
                this->mDataSaveParam.TimeOrderLast = mDataSaveParam.TimeOrder;
            }
        }
    }
    return 1;
}

